package sokoban;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;

public class Input {
	
	private int max;
	private char[][] grid;
	private boolean[][] targets;
	private String levelName;
	private String map;
	private String[] currentMap;
	private ArrayList<String> hsLines;
	HighscoreList hs;
	private String input;
	public Input(){
		
	}
	public String getLevelName(){
		return levelName;
	}
	public void read (InputStream inStream) throws IOException{		
		hs = new HighscoreList();
		hsLines= new ArrayList<String>();
		map="";
		BufferedReader r = new BufferedReader(new InputStreamReader(inStream));
		String s=r.readLine();			
		if("HS".equals(s)){
			while(s!=null){
				hsLines.add(s.trim());
				try {
					s=r.readLine();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			readHs();
			
		}
		if(s!=null){
			levelName=s;
		}
		while(s!=null){
			if(s.startsWith("#")||s.startsWith(" ")){
				map+=s;
			}
			s=r.readLine();
		}		
	}
	public void hss() throws IOException{
		File temp = new File("src/sokoban/Highscores/"+input);		
		if(!temp.exists()){
			try {
				temp.createNewFile();
				
			} catch (IOException e) {
				System.out.println("Error in creating file");
				System.out.println(input);
			}
		}
		FileInputStream in = new FileInputStream("src/sokoban/Highscores/"+input);
		hs.setUrl("src/sokoban/Highscores/"+input);
		
		read(in);
		
	}
	public HighscoreList getHs(){
		return hs;
	}
	public void readHs(){
		for(int i=1;i<hsLines.size();i++){			
			String s = hsLines.get(i);
			Scanner sc=new Scanner(s);
			String name = sc.next();
			int score = Integer.parseInt(sc.next());
			SokobanResult sr=new SokobanResult(name,score);
			hs.addResult(sr);			

		}
	}
	public void initiate(String input){				
		if(input!=null&&input.length()>0){
			this.input=input;
			try{
				FileInputStream in = new FileInputStream("src/sokoban/maps/"+input);							
				read(in);
				makeLevel();
			}
			catch(IOException e){
				System.out.println("Filen finnes ikke!");
				new Sokoban();
			}
		}
	}
	
	public void makeLevel(){
		currentMap= map.split("\\|");	
		max= maxwidth(currentMap);		
		grid= new char[currentMap.length][max];
		targets= new boolean[currentMap.length][max];
		for(int i=0;i<grid.length;i++){
			for(int j=0;j<currentMap[i].length();j++){
				grid[i][j]= currentMap[i].charAt(j);
				if(grid[i].length<max){
					for(int k=0;k<max-grid[k].length;k++){
						grid[i][k]=CHARS.WALL;
					}
				}							
			}
		}
		for(int i=0;i<grid.length;i++){
			for(int j=0;j<currentMap[i].length();j++){				
				if(currentMap[i].charAt(j)==CHARS.TARGET||currentMap[i].charAt(j)==CHARS.BOX_ON_TARGET||currentMap[i].charAt(j)==CHARS.PLAYER_ON_TARGET){
					targets[i][j]=true;					
				}							
			}
		}
		try{
			hss();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	int maxwidth(String[] map){
		int max = 0;
		for(int i=0;i<map.length;i++){
			if(map[i].length()>max){
				max=map[i].length();
			}
		}
		return max;
	}
	public char[][] getGrid(){
		return grid;
	}
	public boolean[][] getTargets(){
		return targets;
	}
}
